//***********************************************************************
//	File:		Game.cpp
//	Author:		Doug Monroe
//	Course:		SGD 1404
//	Purpose:	Game class runs the game logic
//***********************************************************************

#include "Game.h"

// SGD Wrappers:
#include "../SGD Wrappers/SGD_AudioManager.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "../SGD Wrappers/SGD_String.h"
#include "../SGD Wrappers/SGD_EventManager.h"
#include "../SGD Wrappers/SGD_MessageManager.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <ShlObj.h>
#include <Shlwapi.h>
#pragma comment( lib, "shlwapi.lib")
#include "Player.h"
#include "stdafx.h"
#include <random>
#include "MainMenuState.h"
#include "IGameState.h"
#include "BitmapFont.h"
#include "GameplayState.h"
#include "TileSystem.h"
#include "Emitter.h"

#define ARCADE 0
#define GAMEPAD_A 0
#define GAMEPAD_B 1
#define GAMEPAD_X 2
#define GAMEPAD_Y 3
#define GAMEPAD_LB 4
#define GAMEPAD_PAUSE 7

//***********************************************************************
// SINGLETON
//	- instantiation of the static member
/*static*/ Game* Game::s_pInstance = nullptr;

// GetInstance
//	- allocate ONE object
//	- return the single object
/*static*/ Game* Game::GetInstance( void )
{
	if( s_pInstance == nullptr )
		s_pInstance = new Game;

	return s_pInstance;
}

// DeleteInstance
//	- deallocate the single object
/*static*/ void  Game::DeleteInstance( void )
{
	delete s_pInstance;
	s_pInstance = nullptr;
}

/**/
//***********************************************************************
// Initialize
//	- called ONCE when the game starts
//	- initialize the SGD Wrappers
//	- load assets/resources
//	- allocate / setup game objects
bool Game::Initialize( float width, float height )
{
	// Seed First!
	srand( (unsigned int)time( nullptr ) );
	rand();
	
	LoadAudioFile();	

	LoadHighScores();

	// Create local pointers to the SGD Wrappers
	SGD::AudioManager*		pAudio		= SGD::AudioManager::GetInstance();
	SGD::GraphicsManager*	pGraphics	= SGD::GraphicsManager::GetInstance();
	SGD::InputManager*		pInput		= SGD::InputManager::GetInstance();

	// Initialize the wrappers
	if( pAudio->Initialize() == false 
		|| pGraphics->Initialize( true ) == false
		|| pInput->Initialize() == false )
		return false;

	pAudio->SetMasterVolume(SGD::AudioGroup::Music, m_MusicVol);
	pAudio->SetMasterVolume(SGD::AudioGroup::SoundEffects, m_SoundFXVol);

	// Store the parameters
	m_fScreenWidth	= width;
	m_fScreenHeight = height;

	// Dark gray background
//	pGraphics->SetClearColor( { 64, 64, 64 } );

	if (GetWindowd())
		SGD::GraphicsManager::GetInstance()->Resize({ GetScreenWidth(), GetScreenHeight() }, !GetWindowd());
	
	//Load in all of the music and menu sounds as soon as the game starts
	music[MENU] = pAudio->LoadAudio("resource/Sounds/Music/menuTheme.xwm");
	music[HUB] = pAudio->LoadAudio("resource/Sounds/Music/hubTheme.xwm");
	music[FOREST] = pAudio->LoadAudio("resource/Sounds/Music/ForestTheme.xwm");
	music[CAVE] = pAudio->LoadAudio("resource/Sounds/Music/CaveTheme.xwm");
	music[CASTLE] = pAudio->LoadAudio("resource/Sounds/Music/CastleTheme.xwm");
	music[VOLCANO] = pAudio->LoadAudio("resource/Sounds/Music/VolcanoTheme.xwm");

	menuSelectionChange = pAudio->LoadAudio("resource/Sounds/MenuSelectionChanged.wav");
	menuSelectionConfirm = pAudio->LoadAudio("resource/Sounds/MenuSelectionConfirmed.wav");

	soundEffects[FIREBLAST] = pAudio->LoadAudio("resource/Sounds/fireball.wav");
	soundEffects[EXPLOSIVE] = pAudio->LoadAudio("resource/Sounds/explosive.wav");
	soundEffects[PORTAL] = pAudio->LoadAudio("resource/Sounds/portal.wav");
	soundEffects[COMBOBREAKER] = pAudio->LoadAudio("resource/Sounds/cccomboBreaker.wav");
	soundEffects[ARCANESHOT]= pAudio->LoadAudio("resource/Sounds/ShootingSound.wav");
	soundEffects[BEAM]= pAudio->LoadAudio("resource/Sounds/beamSFX.wav");
	soundEffects[ELECTRON]= pAudio->LoadAudio("resource/Sounds/electronSFX.wav");
	soundEffects[PHOTON]= pAudio->LoadAudio("resource/Sounds/ricochetSFX.wav");
	soundEffects[SHOCKWAVE]= pAudio->LoadAudio("resource/Sounds/shockwaveSFX.wav");
	soundEffects[VAMPIRIC]= pAudio->LoadAudio("resource/Sounds/vampiricSFX.wav");
	soundEffects[DOUBLE]= pAudio->LoadAudio("resource/Sounds/doubleSFX.wav");
	soundEffects[SOLARFLARE]= pAudio->LoadAudio("resource/Sounds/solarflareSFX.wav");
	soundEffects[SEEKER]= pAudio->LoadAudio("resource/Sounds/sludgeSFX.wav");
	soundEffects[MAGICMISSILE] = pAudio->LoadAudio("resource/Sounds/magicmissileSFX.wav");
	soundEffects[SMALLCOMBOBREAK] = pAudio->LoadAudio("resource/Sounds/smallcomboBreak.wav");


	enemyDamaged = pAudio->LoadAudio("resource/Sounds/enemyDamaged.wav");
	//Load in the background image when the game starts
	menuBackground = pGraphics->LoadTexture("resource/Graphics/MenuBackground.png");
	forestBackground = pGraphics->LoadTexture("resource/Graphics/ForestBackground.jpg");

	m_hControllerimage = SGD::GraphicsManager::GetInstance()->LoadTexture("resource/Graphics/ControllerSetup.png");
	m_hInstructions = SGD::GraphicsManager::GetInstance()->LoadTexture("resource/Graphics/basicinstructions.png");
	
	// set cursor image
	cursorImage = pGraphics->LoadTexture("resource/Graphics/wand.png");

	//Set the Gamestate
	ChangeState(MainMenuState::GetInstance());

	// Allocate & initialize the font
	m_pFont = new BitmapFont;
	m_pFont->Initialize();

	//emitter1->LoadEmitterXML("resource/ParticleFiles/SmokeyEffect");

	// Store the starting time
	m_ulGameTime = GetTickCount();
	return true;
}

//***********************************************************************
// Update
//	- called EACH FRAME
//	- update each SGD Wrapper
//	- update all game objects
int Game::Update( void )
{
	// Create local pointers to the SGD Wrappers
	SGD::AudioManager*		pAudio		= SGD::AudioManager::GetInstance();
	SGD::GraphicsManager*	pGraphics	= SGD::GraphicsManager::GetInstance();
	SGD::InputManager*		pInput		= SGD::InputManager::GetInstance();

	// Update the wrappers
	if( pAudio->Update() == false 
		|| pGraphics->Update() == false
		|| pInput->Update() == false )
		return -10;		// exit FAILURE!


	// Press 'Esc' to quit
	//if( pInput->IsKeyPressed( SGD::Key::Escape ) == true )
		//return +1;		// exit SUCCESS!


	// Calculate the elapsed time between frames
	unsigned long now = GetTickCount();					// current time in milliseconds
	elapsedTime = (now - m_ulGameTime) / 1000.0f;	// convert to fraction of a second
	m_ulGameTime = now;									// store the current time
	
	if (elapsedTime < 0.000001f)
		elapsedTime = 0.000001f;
	else if (elapsedTime > 0.1)
		elapsedTime = 0.1f;


	

	// Update the FPS
	//++m_unFrames;
	//m_fFPSTimer += elapsedTime;
	//
	//if( m_fFPSTimer >= 1.0f )	// 1 second elapsed
	//{
	//	m_unFPS		= m_unFrames;
	//	m_unFrames	= 0;
	//	m_fFPSTimer = 0.0f;
	//}
	
	//// Print the variable into a stringstream
	//SGD::OStringStream output;
	//output << "FPS: " << m_unFPS;
	//pGraphics->DrawString( output.str().c_str(), { 0, 0 } );

	// Let the current state handle input
	if (m_pCurrState->Input() == false)
		return 1;	// exit success!

	// Update & render the current state
	m_pCurrState->Update(elapsedTime);
	m_pCurrState->Render();

	return 0;	// keep running
}

//***********************************************************************
// Terminate
//	- called ONCE when the game exits
//	- deallocate game objects
//	- unload assets / resources
//	- terminate each SGD Wrapper
void Game::Terminate( void )
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hControllerimage);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hInstructions);

	SaveAudioFile();
	// Exit the current state
	ChangeState(nullptr);

	// Create local pointers to the SGD Wrappers
	SGD::AudioManager*		pAudio		= SGD::AudioManager::GetInstance();
	SGD::GraphicsManager*	pGraphics	= SGD::GraphicsManager::GetInstance();
	SGD::InputManager*		pInput		= SGD::InputManager::GetInstance();

	pAudio->StopAudio(music[MENU]);
	
	for (int i = 0; i < numSongs; i++)
	{
		pAudio->UnloadAudio(music[i]);
	}

	pAudio->UnloadAudio(menuSelectionChange);
	pAudio->UnloadAudio(menuSelectionConfirm);
	pAudio->UnloadAudio(enemyDamaged);

	for (int i = 0; i < NUM_SFX; i++)
	{
		pAudio->UnloadAudio(soundEffects[i]);
	}

	pGraphics->UnloadTexture(menuBackground);
	pGraphics->UnloadTexture(forestBackground);
	// set cursor image
	pGraphics->UnloadTexture(cursorImage);


	AnimationSystem::GetInstance()->UnloadTexture();
	AnimationSystem::GetInstance()->DeleteInstance();
	// Terminate & deallocate the font
	m_pFont->Terminate();
	delete m_pFont;
	m_pFont = nullptr;

	pAudio->Terminate();
	SGD::AudioManager::DeleteInstance();
	pAudio = nullptr;

	pGraphics->Terminate();
	SGD::GraphicsManager::DeleteInstance();
	pGraphics = nullptr;

	pInput->Terminate();
	SGD::InputManager::DeleteInstance();
	pInput = nullptr;
}
void Game::ChangeState(IGameState* pNewState)
{
	// Exit the old state
	if (m_pCurrState != nullptr)
		m_pCurrState->Exit();

	// Store the new state
	m_pCurrState = pNewState;

	// Enter the new state
	if (m_pCurrState != nullptr)
		m_pCurrState->Enter();
}
void Game::LoadAudioFile()
{
	wchar_t* path = nullptr;
	SHGetKnownFolderPath(FOLDERID_LocalAppData, NULL, NULL, &path);
	wstring filename = path;
	filename.append(L"\\MegaLands\\");
	char newStr[300];
	wcstombs_s(nullptr, newStr, filename.c_str(), 300);

	filename.append(L"audio.txt");

	ifstream ifs{ filename, ios::in };
	int windowds;
	ifs >> (int)m_SoundFXVol >> (int)m_MusicVol >> (int)windowds;
	if (windowds == 0)
		m_bWindowd = false;
	else
		m_bWindowd = true;
	ifs.close();
}
void Game::SaveAudioFile()
{
	wchar_t* path = nullptr;
	SHGetKnownFolderPath(FOLDERID_LocalAppData, NULL, NULL, &path);
	wstring filename = path;
	filename.append(L"\\MegaLands\\");
	char newStr[300];
	wcstombs_s(nullptr, newStr, filename.c_str(), 300);

	//Create directory if it does not exist
	if (!PathFileExists(filename.c_str()))
		CreateDirectory(filename.c_str(), NULL);

	filename.append(L"audio.txt");

	ofstream ofs{ filename, ios::out | ios::trunc };
	ofs << m_SoundFXVol << ' ' << m_MusicVol << ' ' << m_bWindowd;
	ofs.close();
}

void Game::SetCameraPos(SGD::Point position, SGD::Rectangle getrect)
{
	TileSystem * TS = TileSystem::GetInstance();
	GameplayState * Gs = GameplayState::GetInstance();
	
	if (Gs->GetPlayer()->GetMinPosInWorld() == 0.0f)
		bossArea = false;
	else if (Gs->GetCurrLevel() == int(Levels::VOLCANO) && Gs->GetPlayer()->GetPosition().x > m_fScreenWidth - Gs->GetPlayer()->GetSize().width && !bossArea)
		bossArea = true;
	if (bossArea)
	{
		if (m_pCameraPos.x < m_fScreenWidth)
		{
			m_pCameraPos.x += 250 * elapsedTime;
			Gs->GetPlayer()->SetMinPosInWorld(m_pCameraPos.x);
		}
		else
		{
			m_pCameraPos = SGD::Point(m_fScreenWidth, 0);
		}
	}
	else
	{
		m_pCameraPos.x = position.x - m_fScreenWidth / 2;
		if (m_pCameraPos.x < 0)
		{
			m_pCameraPos.x = 0;
		}
		else if (m_pCameraPos.x > TS->GetMapWidth(Gs->GetMapIndex()) - m_fScreenWidth) // USE GAMEPLAY STATE TO PASS IN LEVEL ID
		{
			m_pCameraPos.x = TS->GetMapWidth(Gs->GetMapIndex()) - m_fScreenWidth;
		}

		m_pCameraPos.y = (int(position.y) / int(m_fScreenHeight)) * m_fScreenHeight;
	}
}

#pragma region Each button input method
#if ARCADE
bool Game::FireInput()
{
	if (pInput->IsButtonDown(0, 0) == true)
		return true;
	return false;
}

bool Game::JumpInput()
{
	if (pInput->IsButtonPressed(0, 1))
		return true;
	return false;
}

bool Game::MovementAbilityInput()
{
	if (pInput->IsButtonPressed(0, 2))
		return true;
	return false;
}

bool Game::LeftInput(SGD::Vector tempvec)
{
	if (tempvec.x <= -0.8f)
		return true;
	return false;
}

bool Game::RightInput(SGD::Vector tempvec)
{
	if (tempvec.x >= 0.8f)
		return true;
	return false;
}

bool Game::UpInput(SGD::Vector tempvec)
{
	if (tempvec.y <= -0.8f)
		return true;
	return false;
}

bool Game::UpInputPressed(SGD::Vector tempvec)
{
	if (tempvec.y <= -0.8f && !upPressed)
	{
		upPressed = true;
		return true;
	}
	else if (tempvec.y > -0.8f)
		upPressed = false;
	return false;
}

bool Game::DownInput(SGD::Vector tempvec)
{
	if (tempvec.y >= 0.9f)
		return true;
	return false;
}

bool Game::HoldPositionInput()
{
	if (pInput->IsButtonDown(0, 3))
		return true;
	return false;
}

bool Game::HotSwapUpInput()
{
	if (pInput->GetLeftJoystick(0).y <= -0.8f && pInput->IsButtonDown(0,4) && !RStickupPressed)
	{
		RStickupPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).y > -0.8f)
		RStickupPressed = false;
	return false;
}

bool Game::HotSwapRightInput()
{
	if (pInput->GetLeftJoystick(0).x >= 0.8f && pInput->IsButtonDown(0, 4) && !RStickrightPressed)
	{
		RStickrightPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).x < 0.8f)
		RStickrightPressed = false;
	return false;
}

bool Game::HotSwapDownInput()
{
	if (pInput->GetLeftJoystick(0).y <= 0.8f && pInput->IsButtonDown(0, 4) && !RStickdownPressed)
	{
		RStickdownPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).y < 0.8f)
		RStickdownPressed = false;
	return false;
}

bool Game::HotSwapLeftInput()
{
	if (pInput->GetLeftJoystick(0).x <= -0.8f && pInput->IsButtonDown(0, 4) && !RStickleftPressed)
	{
		RStickleftPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).x > -0.8f)
		RStickleftPressed = false;
	return false;
}

bool Game::MenuUpInput()
{
	if (pInput->GetLeftJoystick(0).y <= -0.8f && !upPressed)
	{
		upPressed = true;
		SGD::AudioManager::GetInstance()->PlayAudio(menuSelectionChange);
		return true;
	}
	else if (pInput->GetLeftJoystick(0).y > -0.8f)
		upPressed = false;
	return false;
}

bool Game::MenuDownInput()
{
	if (pInput->GetLeftJoystick(0).y >= 0.8f && !downPressed)
	{
		downPressed = true;
		SGD::AudioManager::GetInstance()->PlayAudio(menuSelectionChange);
		return true;
	}
	else if (pInput->GetLeftJoystick(0).y < 0.8f)
		downPressed = false;
	return false;
}

bool Game::MenuRightInput()
{
	if (pInput->GetLeftJoystick(0).x >= 0.8f && !rightPressed)
	{
		rightPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).x < 0.8f)
		rightPressed = false;
	return false;
}

bool Game::MenuLeftInput()
{
	if (pInput->GetLeftJoystick(0).x <= -0.8f && !leftPressed)
	{
		leftPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).x > -0.8f)
		leftPressed = false;
	return false;
}

bool Game::MenuConfirmInput()
{
	if (pInput->IsKeyPressed(SGD::Key::Enter) || pInput->IsButtonPressed(0, 0))
		return true;
	return false;
}

bool Game::MenuBackInput()
{
	if (pInput->IsKeyPressed(SGD::Key::Escape) || pInput->IsButtonPressed(0, 1))
		return true;
	return false;
}

bool Game::MenuDeleteInput()
{
	if (pInput->IsKeyPressed(SGD::Key::Backspace) || pInput->IsKeyPressed(SGD::Key::Delete) || pInput->IsButtonPressed(0, GAMEPAD_B))
		return true;
	return false;
}

bool Game::PauseInput()
{
	if (pInput->IsKeyPressed(SGD::Key::MouseLeft) || pInput->IsButtonPressed(0, 6))
		return true;
	return false;
}

bool Game::TableCycleRightInput()
{
	if (pInput->GetLeftJoystick(0).x >= 0.8f && !rightPressed)
	{
		rightPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).x < 0.8f)
		rightPressed = false;
	return false;
}

bool Game::TableCycleLeftInput()
{
	if (pInput->GetLeftJoystick(0).x <= -0.8f && !leftPressed)
	{
		leftPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).x > -0.8f)
		leftPressed = false;
	return false;
}
bool Game::ExitTableInput()
{
	if (pInput->IsKeyPressed(SGD::Key::W))
		return true;
	if (pInput->GetLeftJoystick(0).y <= -0.8f && !upPressed)
	{
		upPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).y > -0.8f)
		upPressed = false;
	return false;
}
#else
bool Game::FireInput()
{
	if (pInput->IsKeyDown(SGD::Key::J) || pInput->IsButtonDown(0, GAMEPAD_X) == true)
		return true;
	return false;
}

bool Game::JumpInput()
{
	if (pInput->IsButtonPressed(0, GAMEPAD_A) || pInput->IsKeyPressed(SGD::Key::K))
		return true;
	return false;
}

bool Game::MovementAbilityInput()
{
	if ((pInput->IsKeyPressed(SGD::Key::L) || pInput->IsButtonPressed(0, GAMEPAD_B)))
		return true;
	return false;
}

bool Game::LeftInput(SGD::Vector tempvec)
{
	if (pInput->IsKeyDown(SGD::Key::A) || tempvec.x <= -0.8f)
		return true;
	return false;
}

bool Game::RightInput(SGD::Vector tempvec)
{
	if (pInput->IsKeyDown(SGD::Key::D) || tempvec.x >= 0.8f)
		return true;
	return false;
}

bool Game::UpInput(SGD::Vector tempvec)
{
	if (pInput->IsKeyDown(SGD::Key::W) || tempvec.y <= -0.8f)
		return true;
	return false;
}

bool Game::UpInputPressed(SGD::Vector tempvec)
{
	if (pInput->IsKeyPressed(SGD::Key::W))//need to simulate the control stick being pressed instead of being held down. Will do when there is a need to do so.
		return true;
	if (tempvec.y <= -0.8f && !upPressed)
	{
		upPressed = true;
		return true;
	}
	else if (tempvec.y > -0.8f)
		upPressed = false;
	return false;
}

bool Game::DownInput(SGD::Vector tempvec)
{
	if (pInput->IsKeyDown(SGD::Key::S) || tempvec.y >= 0.9f)
		return true;
	return false;
}

bool Game::HoldPositionInput()
{
	if ((pInput->IsKeyDown(SGD::Key::Shift) || pInput->IsButtonDown(0, 5))/* && gravityOn == false*/)
		return true;
	return false;
}

bool Game::HotSwapUpInput()
{
	if (pInput->IsKeyPressed(SGD::Key::UpArrow) || pInput->IsDPadPressed(0, SGD::DPad::Up) || pInput->IsKeyPressed(SGD::Key::One))
		return true;
	if (pInput->GetRightJoystick(0).y <= -0.8f && !RStickupPressed)
	{
		RStickupPressed = true;
		return true;
	}
	else if (pInput->GetRightJoystick(0).y > -0.8f)
		RStickupPressed = false;
	return false;
}

bool Game::HotSwapRightInput()
{
	if (pInput->IsKeyPressed(SGD::Key::RightArrow) || pInput->IsDPadPressed(0, SGD::DPad::Right) || pInput->IsKeyPressed(SGD::Key::Two))
		return true;
	if (pInput->GetRightJoystick(0).x >= 0.8f && !RStickrightPressed)
	{
		RStickrightPressed = true;
		return true;
	}
	else if (pInput->GetRightJoystick(0).x < 0.8f)
		RStickrightPressed = false;
	return false;
}

bool Game::HotSwapDownInput()
{
	if (pInput->IsKeyPressed(SGD::Key::DownArrow) || pInput->IsDPadPressed(0, SGD::DPad::Down) || pInput->IsKeyPressed(SGD::Key::Three))
		return true;
	if (pInput->GetRightJoystick(0).y >= 0.8f && !RStickdownPressed)
	{
		RStickdownPressed = true;
		return true;
	}
	else if (pInput->GetRightJoystick(0).y < 0.8f)
		RStickdownPressed = false;
	return false;
}

bool Game::HotSwapLeftInput()
{
	if (pInput->IsKeyPressed(SGD::Key::LeftArrow) || pInput->IsDPadPressed(0, SGD::DPad::Left) || pInput->IsKeyPressed(SGD::Key::Four))
		return true;
	if (pInput->GetRightJoystick(0).x <= -0.8f && !RStickleftPressed)
	{
		RStickleftPressed = true;
		return true;
	}
	else if (pInput->GetRightJoystick(0).x > -0.8f)
		RStickleftPressed = false;
	return false;
}

bool Game::MenuUpInput()
{
	if (pInput->IsKeyPressed(SGD::Key::UpArrow))
	{
		SGD::AudioManager::GetInstance()->PlayAudio(menuSelectionChange);
		return true;
	}
	if (pInput->GetLeftJoystick(0).y <= -0.8f && !upPressed)
	{
		upPressed = true;
		SGD::AudioManager::GetInstance()->PlayAudio(menuSelectionChange);
		return true;
	}
	else if (pInput->GetLeftJoystick(0).y > -0.8f)
		upPressed = false;
	return false;
}

bool Game::MenuDownInput()
{
	if (pInput->IsKeyPressed(SGD::Key::DownArrow))
	{
		SGD::AudioManager::GetInstance()->PlayAudio(menuSelectionChange);
		return true;
	}
	if (pInput->GetLeftJoystick(0).y >= 0.8f && !downPressed)
	{
		downPressed = true;
		SGD::AudioManager::GetInstance()->PlayAudio(menuSelectionChange);
		return true;
	}
	else if (pInput->GetLeftJoystick(0).y < 0.8f)
		downPressed = false;
	return false;
}

bool Game::MenuRightInput()
{
	if (pInput->IsKeyPressed(SGD::Key::RightArrow))
		return true;
	if (pInput->GetLeftJoystick(0).x >= 0.8f && !rightPressed)
	{
		rightPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).x < 0.8f)
		rightPressed = false;
	return false;
}

bool Game::MenuLeftInput()
{
	if (pInput->IsKeyPressed(SGD::Key::LeftArrow))
		return true;
	if (pInput->GetLeftJoystick(0).x <= -0.8f && !leftPressed)
	{
		leftPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).x > -0.8f)
		leftPressed = false;
	return false;
}

bool Game::MenuConfirmInput()
{
	if (pInput->IsKeyPressed(SGD::Key::Enter) || pInput->IsButtonPressed(0, GAMEPAD_A))
		return true;
	return false;
}

bool Game::MenuBackInput()
{
	if (pInput->IsKeyPressed(SGD::Key::Escape) || pInput->IsButtonPressed(0, GAMEPAD_B) || pInput->IsKeyPressed(SGD::Key::B))
		return true;
	return false;
}

bool Game::MenuDeleteInput()
{
	if (pInput->IsKeyPressed(SGD::Key::Backspace) || pInput->IsKeyPressed(SGD::Key::Delete) || pInput->IsButtonPressed(0, GAMEPAD_B))
		return true;
	return false;
}

bool Game::PauseInput()
{
	if (pInput->IsKeyPressed(SGD::Key::Escape) || pInput->IsButtonPressed(0, GAMEPAD_PAUSE))
		return true;
	return false;
}

bool Game::TableCycleRightInput()
{
	if (pInput->IsKeyPressed(SGD::Key::D))
		return true;
	if (pInput->GetLeftJoystick(0).x >= 0.8f && !rightPressed)
	{
		rightPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).x < 0.8f)
		rightPressed = false;
	return false;
}

bool Game::TableCycleLeftInput()
{
	if (pInput->IsKeyPressed(SGD::Key::A))
		return true;
	if (pInput->GetLeftJoystick(0).x <= -0.8f && !leftPressed)
	{
		leftPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).x > -0.8f)
		leftPressed = false;
	return false;
}

bool Game::ExitTableInput()
{
	if (pInput->IsKeyPressed(SGD::Key::W))
		return true;
	if (pInput->GetLeftJoystick(0).y <= -0.8f && !upPressed)
	{
		upPressed = true;
		return true;
	}
	else if (pInput->GetLeftJoystick(0).y > -0.8f)
		upPressed = false;
	return false;
}
#endif
#pragma endregion

void Game::LoadHighScores()
{
	wchar_t* path = nullptr;
	SHGetKnownFolderPath(FOLDERID_LocalAppData, NULL, NULL, &path);
	wstring filename = path;
	filename.append(L"\\MegaLands\\");
	char newStr[300];
	wcstombs_s(nullptr, newStr, filename.c_str(), 300);

	wstring forest = filename;
	forest.append(L"forest.txt");
	wstring cave = filename;
	cave.append(L"cave.txt");
	wstring castle = filename;
	castle.append(L"castle.txt");
	wstring volcano = filename;
	volcano.append(L"volcano.txt");

	ofstream ofs;

	//Create directory if it does not exist
	if (!PathFileExists(forest.c_str()))
	{
		ofs.open(forest, ios_base::trunc);
		if (ofs.is_open())
		{
			ofs << "Forest" << endl;
			for (int i = 0; i < 10; i++)
			{
				ofs << "" << '\t' << 0 << '\t' << 0 << endl;
			}
		}
		ofs.close();
		ofs.clear();
	}
	
	if (!PathFileExists(cave.c_str()))
	{
		ofs.open(cave, ios_base::trunc);
		if (ofs.is_open())
		{
			ofs << "Cave" << endl;
			for (int i = 0; i < 10; i++)
			{
				ofs << "" << '\t' << 0 << '\t' << 0 << endl;
			}
		}
		ofs.close();
		ofs.clear();
	}

	if (!PathFileExists(castle.c_str()))
	{
		ofs.open(castle, ios_base::trunc);
		if (ofs.is_open())
		{
			ofs << "Castle" << endl;
			for (int i = 0; i < 10; i++)
			{
				ofs << "" << '\t' << 0 << '\t' << 0 << endl;
			}
		}
		ofs.close();
		ofs.clear();
	}

	if (!PathFileExists(volcano.c_str()))
	{
		ofs.open(volcano, ios_base::trunc);
		if (ofs.is_open())
		{
			ofs << "Volcano" << endl;
			for (int i = 0; i < 10; i++)
			{
				ofs << "" << '\t' << 0 << '\t' << 0 << endl;
			}
		}
	}

	ifstream fs;
	fs.open(forest);
	if (fs.is_open())
	{
		getline(fs, levelNames[0], '\n');
		for (int i = 0; i < 10; i++)
		{
			getline(fs, names[0][i], '\t');
			fs >> combos[0][i];
			fs >> scores[0][i];
			fs.ignore(INT_MAX, '\n');
		}
		fs.close();
	}
	fs.clear();

	fs.open(cave);
	if (fs.is_open())
	{
		getline(fs, levelNames[1], '\n');
		for (int i = 0; i < 10; i++)
		{
			getline(fs, names[1][i], '\t');
			fs >> combos[1][i];
			fs >> scores[1][i];
			fs.ignore(INT_MAX, '\n');
		}
		fs.close();
	}
	fs.clear();

	fs.open(castle);
	if (fs.is_open())
	{
		getline(fs, levelNames[2], '\n');
		for (int i = 0; i < 10; i++)
		{
			getline(fs, names[2][i], '\t');
			fs >> combos[2][i];
			fs >> scores[2][i];
			fs.ignore(INT_MAX, '\n');
		}
		fs.close();
	}
	fs.clear();

	fs.open(volcano);
	if (fs.is_open())
	{
		getline(fs, levelNames[3], '\n');
		for (int i = 0; i < 10; i++)
		{
			getline(fs, names[3][i], '\t');
			fs >> combos[3][i];
			fs >> scores[3][i];
			fs.ignore(INT_MAX, '\n');
		}
		fs.close();
	}
	fs.clear();
}

void Game::PlaySFX(int sfx)
{
	SGD::AudioManager::GetInstance()->PlayAudio(soundEffects[sfx]);
}